// pages/userManage/userManage.js
Page({
  
  /**
   * 页面的初始数据
   */
  data: {
    noManager:false,
    noConnect:false,
    windowOn:false,
    is_add:false,
    now_list:[],
    total_list:[],
    window_info:[],
    name:"",
    complete_num:0,
    correct:0,
    incorrect:0,
    path:"",
    id:0,
    info:"",
    img:""
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var openid = wx.getStorageSync('userinfo').openid
    wx.request({
      url:getApp().globalData.url+"getquestionlist/",
      data:{
          search:"",
          openid:openid
      },
      header:{
        "Content-Type":"application/json"
      },
      success:function(res){
        if(res.data == "not manager"){
          that.setData({
            now_list:[],
            total_list:[],
            noManager:true,
            windowOn:false,
          
        })
        }else{
          that.setData({
            now_list:res.data,
            total_list:res.data,
            windowOn:false,
            
          })
        }
      },
      fail:function(res){
        that.setData({
          noConnect:true
        })
      }
  })
},
searchButton:function(e){
  var that = this
  var search = e.detail.value
  wx.request({
    url:getApp().globalData.url+"getquestionlist/",
    data:{
        search:search,
        openid:wx.getStorageSync('userinfo').openid
    },
    header:{
      "Content-Type":"application/json"
    },
    success:function(res){
      console.log(res)
      if(res.data == "not manager"){
        that.setData({
          now_list:[],
          total_list:[],
          noManager:true,
          windowOn:false,
        
      })
      }else{
        that.setData({
          now_list:res.data,
          total_list:res.data,
          windowOn:false,
          
        })
      }
    },
    fail:function(res){
      that.setData({
        noConnect:true
      })
    }
})
},


  edite:function(e){
    var that = this
    var id = parseInt(e.currentTarget.id)
    var list = that.data.now_list
    for(var i=0 ;i<list.length;i++){
      if(list[i].id == id){
        that.setData({
          window_info:list[i],
          name:list[i].name,
          correct:list[i].correct_num,
          incorrect:list[i].incorrect_num,
          id:list[i].id,
          info:list[i].info,
          complete_num:list[i].complete_num,
          path:"",
          is_add:false,
          img:list[i].img
        })
      }
    }
    that.setData({
      windowOn:true
    })
  },
  add:function(e){
    var that = this
    that.setData({
      window_info:[],
      name:"",
      correct:0,
      incorrect:0,
      id:0,
      info:"",
      complete_num:0,
      path:"",
      windowOn:true,
      is_add:true,
      img:""
    })
  
  },



  cancel:function(e){
    var that = this
    that.setData({
      windowOn:false
    })

  },
  
  save:function(e){
    var that = this


    wx.showModal({

      content: '确认保存？',
      success: function (res) {
        if(that.data.is_add){
          var type = "addquestion"
        }else{
          var type = "setquestion"
        }
        if (res.confirm) {  
          var openid = wx.getStorageSync('userinfo').openid
          wx.request({
            url:getApp().globalData.url+"savequestion/",
            data:{
              type:type,
              openid:openid,
              id:that.data.id,
              info:that.data.info,
              name:that.data.name,
              complete_num:that.data.complete_num,
              correct_num:that.data.correct,
              incorrect_num:that.data.incorrect,
              img:that.data.img
            },
            header:{
              "Content-Type":"application/json"
            },
            
            success:function(res){
              if(type == "addquestion"){
                var id= res.data[0]
              }else if(type == "setquestion"){
                var id = that.data.id
              }
              console.log(id)
              console.log(that.data.path)
              if(that.data.path != ""){
                wx.uploadFile({
                  filePath: that.data.path,
                  name: 'file',
                  url: getApp().globalData.url+"getfile/",
                  formData:{
                    id:id,
                    openid:wx.getStorageSync('userinfo').openid
                  },
                  success:function(res){
                  }
                })
              }

              
              that.onLoad()
            },
            fail:function(res){
      
            }
        })
        }
      }
    })



    
  },



  name:function(e){
    var that = this
    var name = e.detail.value
    that.setData({
      name:name
    })
  },
  info:function(e){
    var that = this
    var info = e.detail.value
    that.setData({
      info:info
    })
  },

  img:function(e){
    var that = this
    var img = e.detail.value
    that.setData({
      img:img
    })
  },
  complete_num:function(e){
    var that = this
    var score = e.detail.value
    that.setData({
      complete_num:complete_num
    })
  },
  correct:function(e){
    var that = this
    var correct = e.detail.value
    that.setData({
      correct:correct
    })
  },

  incorrect:function(e){
    var that = this
    var incorrect = e.detail.value
    that.setData({
      incorrect:incorrect
    })
  },
  
  deleteUser:function(e){
    var that = this

    wx.showModal({

      content: '确认删除题库？',
      success: function (res) {
        if (res.confirm) {  
          var openid = wx.getStorageSync('userinfo').openid
          wx.request({
            url:getApp().globalData.url+"savequestion/",
            data:{
              type:"deletequestion",
              openid:openid,
              id:that.data.window_info.id,
            },
            header:{
              "Content-Type":"application/json"
            },
            
            success:function(res){
              that.onLoad()
            },
            fail:function(res){
      
            }
        })
        }
      }
    })
  },
  hit:function(e){
    var that = this
    wx.chooseMessageFile({
      count: 1,
      type:"file",
      extension:["xlsx"],
      
      success:function(res){
        var tempFilePaths = res.tempFiles[0].path
        that.setData({
          path:tempFilePaths
        })
      }
    })
  },

})